import {createApp} from 'vue'
import App from './App.vue'
import axios from 'axios'
import router from './router'
import store from './store'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

axios.defaults.baseURL = 'http://192.168.1.12:1000/apis/'

// 请求拦截器
axios.interceptors.request.use(
    (config) => {
        config.headers['authori-zation'] = localStorage.getItem('authToken')
        return config
    },
    (error) => {
        return Promise.reject(error)
    }
)

axios.interceptors.response.use(
    (response) => {
        return response;  // Return response data when it's a successful request
    },
    (error) => {
        const res = error.response;
        if (res && res.data.code === 401) {
            localStorage.removeItem('authToken');  // Clear the stored token
            router.push('/login');  // Redirect to login page
        }
        return Promise.reject(error);
    }
);

const app = createApp(App)
app.use(ElementPlus)
app.use(router)
app.use(store)
app.mount('#app')
